package com.github.controller.cms;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.module.cms.Role;
import com.github.pagehelper.PageInfo;
import com.github.service.cms.RoleService;

/**
 * <p>User: Mu Yunlong
 * <p>Date: 2016年2月15日 上午11:50:42
 * <p>Version: 1.0
 */
@RestController
public class RoleController {

    private String page_list = "role_list";
    private String redirect_list = "redirect:list";
    @Autowired
    RoleService roleService;  

//-------------------Retrieve All Users--------------------------------------------------------
    
    @RequestMapping(value = {"role", "role_list.html","/role/list"}, method = RequestMethod.GET)
    public ModelAndView  listAllRoles(Role role,@RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int rows) {
        ModelAndView result = new ModelAndView(page_list);
        List<Role> roles = roleService.findAllRoles();
        result.addObject("pageInfo", new PageInfo<Role>(roles));
        result.addObject("queryParam", role);
        result.addObject("page", page);
        result.addObject("rows", rows);
        return result;
    }


    
    //-------------------Create a User--------------------------------------------------------
    @RequestMapping(value = "/role/add", method = RequestMethod.GET)
    public ModelAndView createRole(Role role) {
        ModelAndView result = new ModelAndView("role_add");
        return result;
    }
    
    @RequestMapping(value = "/role/addDo", method = RequestMethod.POST)
    public ModelAndView createRoleDo( Role role) {
        ModelAndView result = new ModelAndView(redirect_list);
        role.setFlag("1");
        role.setIsedit("0");
        roleService.saveRole(role);
        return result;
    }

    
    //------------------- Update a Role --------------------------------------------------------
    
    @RequestMapping(value = "/role/modify/{roleid}", method = RequestMethod.GET)
    public ModelAndView updateRole(@PathVariable("roleid") long roleid,  Role role) {
        ModelAndView result = new ModelAndView("role_modify");        
        Role currentRole = roleService.findById(roleid);
        result.addObject("role",currentRole);
        return result;
    }
    
    @RequestMapping(value = "/role/modifyDo", method = RequestMethod.POST)
    public ModelAndView updateRoleDo(Role role) {
        ModelAndView result = new ModelAndView(redirect_list);        
        roleService.updateRole(role);
        return result;
    }

    //------------------- Delete a Role --------------------------------------------------------
    
    @RequestMapping(value = "/role/{roleid}", method = RequestMethod.GET)
    public ModelAndView deleteRole(@PathVariable("roleid") long roleid) {
        ModelAndView result = new ModelAndView(redirect_list);        
        roleService.deleteRoleById(roleid);
        return result;
    }

    
   
}
